package com.wyzz.hr.action;

import java.util.HashMap;
import java.util.Map;

import com.wyzz.hr.base.BasePdfExport;
import com.wyzz.hr.base.wf.leaverequest.LrWfApproverAction;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.TemplateService;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.vo.LeaveFormBean;

public class DeptHRApproveLRAction extends LrWfApproverAction {

	public DeptHRApproveLRAction() {
		templateName = "print_leave";
	}

	public String executeInit() throws Exception {
		if (lf_Bean == null)
			lf_Bean = new LeaveFormBean();
		lr = initLfBean("modify", lf_Bean, lrUpdateId);
		if (lr == null)
			return "error";
		else
			return "success";
	}

	public String getLrInfo() {
		if (lf_Bean == null)
			lf_Bean = new LeaveFormBean();
		lr = initLfBean("modify", lf_Bean, lrUpdateId);
		if (lr == null)
			return "error";
		IPositionBaseBo pbBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		PositionBase pb = pbBo.loadPb(lr.getLrEmpNo().getEmpPbNo().getId(),
				new String[] { PositionBase.PROP_PB_JOB_TITLE });
		lr.getLrEmpNo().setEmpPbNo(pb);
		String newTemplateName = getTemplateNameByCat(ltSpecialCat);
		Map modal = new HashMap();
		if ("pdf".equals(resultType))
			lf_Bean.setLrReason(BasePdfExport.processWrap(
					lf_Bean.getLrReason(), 530, 20, 12));
		modal.put("lr", lr);
		modal.put("lf_Bean", lf_Bean);
		TemplateService templateService = (TemplateService) SpringBeanFactory
				.getBean("ftlTemplateService");
		printContent = templateService.getPDFContent(newTemplateName, modal);
		System.out.println(printContent);
		if ("pdf".equals(resultType)) {
			if (printContent.indexOf("&nbsp;") > -1)
				printContent = printContent.replaceAll("&nbsp;", "");
			String fileNameWithPath = BasePdfExport.pdfExport(printContent);
			if (download(fileNameWithPath, (new StringBuilder()).append(
					"leaveInfo_NO").append(lr.getLrNo()).append(".pdf")
					.toString()))
				return "download";
		}
		return "success";
	}

	private String getTemplateNameByCat(String ltSpecialCat) {
		if (ltSpecialCat == null)
			return templateName;
		TemplateService templateService = (TemplateService) SpringBeanFactory
				.getBean("ftlTemplateService");
		String tempName = (new StringBuilder()).append(templateName)
				.append("_").append(ltSpecialCat).toString();
		if (templateService.checkPDFTemplateExist(tempName))
			return tempName;
		else
			return templateName;
	}

	public String getSrcAction() {
		return srcAction;
	}

	public void setSrcAction(String srcAction) {
		this.srcAction = srcAction;
	}

	public String getLrUpdateId() {
		return lrUpdateId;
	}

	public void setLrUpdateId(String lrUpdateId) {
		this.lrUpdateId = lrUpdateId;
	}

	public LeaveFormBean getLf_Bean() {
		return lf_Bean;
	}

	public void setLf_Bean(LeaveFormBean lf_Bean) {
		this.lf_Bean = lf_Bean;
	}

	public Leaverequest getLr() {
		return lr;
	}

	public void setLr(Leaverequest lr) {
		this.lr = lr;
	}

	public int getGmanager() {
		return gmanager;
	}

	public void setGmanager(int gmanager) {
		this.gmanager = gmanager;
	}

	public String getResultType() {
		return resultType;
	}

	public void setResultType(String resultType) {
		this.resultType = resultType;
	}

	public String getPrintContent() {
		return printContent;
	}

	public void setPrintContent(String printContent) {
		this.printContent = printContent;
	}

	public String getLtSpecialCat() {
		return ltSpecialCat;
	}

	public void setLtSpecialCat(String ltSpecialCat) {
		this.ltSpecialCat = ltSpecialCat;
	}

	private static final long serialVersionUID = 1L;
	private LeaveFormBean lf_Bean;
	private Leaverequest lr;
	private String lrUpdateId;
	private int gmanager;
	private String srcAction;
	private String resultType;
	private String templateName;
	private String printContent;
	private String ltSpecialCat;
}
